﻿using Bll;
using DecorationSys.Models;
using Entity;
using Entity.DTO;
using IBll;
using Microsoft.AspNetCore.Mvc;
using NPOI.Util;

namespace DecorationSys.Controllers
{
    /// <summary>
    /// 材料相关
    /// </summary>
    public class MaterialController : Controller
    {
        IMaterialBll _materialBll;
        IClassificationBll _classificationBll;

        public MaterialController(IMaterialBll materialBll, IClassificationBll classificationBll)
        {
            _materialBll = materialBll;
            _classificationBll = classificationBll;
        }

        /// <summary>
        /// 材料列表视图
        /// </summary>
        /// <returns></returns>
        public IActionResult MaterialListView()
        {
            return View();
        }

        /// <summary>
        /// 添加材料视图
        /// </summary>
        /// <returns></returns>
        public IActionResult CreateMaterialView()
        {
            return View();
        }

        /// <summary>
        /// 修改材料视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateMaterialView()
        {
            return View();
        }

        /// <summary>
        /// 材料详情视图
        /// </summary>
        /// <returns></returns>
        public IActionResult DetailsView()
        {
            return View();
        }

        /// <summary>
        /// 根据材料id获取材料信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public MyJsonResult GetMaterialById(string id)
        {
            var material = _materialBll.GetMaterialById(id);

            if (material == null)
            {
                return MyJsonResult.GetErrorResult("材料不存在");
            }
            else
            {
                return MyJsonResult.GetSuccessResult(material);
            }
        }

        /// <summary>
        /// 获取更新订单页面信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public MyJsonResult GetUpdateMaterialDatas(string id)
        {
            var material = _materialBll.GetMaterialById(id);

            var options = _classificationBll.GetSelectOptionList();

            if (material == null)
            {
                return MyJsonResult.GetErrorResult("订单不存在");
            }
            else
            {
                return MyJsonResult.GetSuccessResult(new
                {
                    material,
                    options
                });
            }
        }

        /// <summary>
        /// 更新材料
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult UpdateMaterial(string id, string title, string unit, decimal price, decimal priceMain, decimal priceAssistant, decimal priceMachine, decimal priceArtificial, decimal priceLoss, string remark)
        {
            //参数验证
            if (string.IsNullOrEmpty(id))
            {
                return MyJsonResult.GetErrorResult("材料id不能为空");
            }
            if (string.IsNullOrEmpty(title))
            {
                return MyJsonResult.GetErrorResult("材料名不能为空");
            }
            if (price != priceMain + priceAssistant + priceMachine + priceArtificial + priceLoss)
            {
                return MyJsonResult.GetErrorResult("各费用之和必须等于单价");
            }

            string msg;
            //调用业务逻辑层
            bool isSuccess = _materialBll.UpdateMaterial(id, title, unit, price, priceMain, priceAssistant, priceMachine, priceArtificial, priceLoss, remark, out msg);
            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult(msg);
            }
        }


        /// <summary>
        /// 获取材料列表数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public object GetMaterialList(string title, int page, int limit)
        {
            if (page <= 0)
            {
                return new
                {
                    code = 0,
                    msg = "页码有误",
                    count = 0,
                    data = new List<object>()
                };
            }

            if (limit <= 0)
            {
                return new
                {
                    code = 0,
                    msg = "每页条数有误",
                    count = 0,
                    data = new List<object>()
                };
            }

            int count;

            //查材料数据
            var list = _materialBll.GetMaterialList(title, page, limit, out count);

            return new
            {
                code = 0,
                msg = "cg",
                count,
                data = list
            };
        }

        /// <summary>
        /// 创建材料
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult CreateMaterial(string title, string unit, decimal price, decimal priceMain, decimal priceAssistant, decimal priceMachine, decimal priceArtificial, decimal priceLoss, string remark, string classificationId)
        {
            //参数验证
            if (string.IsNullOrEmpty(title))
            {
                return MyJsonResult.GetErrorResult("请输入材料标题");
            }
            if (string.IsNullOrEmpty(unit))
            {
                return MyJsonResult.GetErrorResult("请输入材料内容");
            }
            if (string.IsNullOrEmpty(classificationId))
            {
                return MyJsonResult.GetErrorResult("请选择分类");
            }
            if (price != priceMain + priceAssistant + priceMachine + priceArtificial + priceLoss)
            {
                return MyJsonResult.GetErrorResult("各费用之和必须等于单价");
            }

            string userId = HttpContext.Session.GetString("UserId");

            //用户id有值的时候才去数据库查菜单数据
            if (string.IsNullOrEmpty(userId))
            {
                return MyJsonResult.GetErrorResult("请重新登录");
            }

            string msg;
            bool isSuccess = _materialBll.CreateMaterial(title, unit, price, priceMain, priceAssistant, priceMachine, priceArtificial, priceLoss, remark, classificationId, out msg);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult(msg);
            }
        }

        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult DeleteMaterial(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return MyJsonResult.GetErrorResult("参数有误");
            }

            bool isSuccess = _materialBll.DeleteMaterial(id);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult("失败");
            }
        }

        /// <summary>
        /// 批量软删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult DeleteMaterials(List<string> ids)
        {
            if (ids.Count <= 0 || ids == null)
            {
                return MyJsonResult.GetErrorResult("参数有误");
            }

            bool isSuccess = _materialBll.DeleteMaterials(ids);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult("失败");
            }
        }

    }

}
